/**
 * Created by Ysssssss on 20/12/15.
 */
const app = getApp();
import { navTo } from '../../../../common/env';
import { deleteProduct } from "../../../../api/product";
import { PACKAGE_PRODUCT_PAGE  } from '../../../../constants/route';

module.exports = Behavior({
    properties: {
        showOperationButton: {
            type: Boolean,
            value: false
        },
    },
    data: {
        show: false,
        operationId: '',
        actions: [
            {
                name: '编辑',
                action: id => navTo(PACKAGE_PRODUCT_PAGE['productEdit'], { id })
            }, {
                name: '删除',
                color: '#FA5151',
                action: id => {
                    let res = deleteProduct({ id })
                    if (!res) return ;
                    app.event.trigger('productEdit')
                }
            }
        ],
    },
    methods: {
        onShow(e) {
            this.setData({
                show: true,
                operationId: e.currentTarget.dataset.id,
            });
        },

        onClose() {
            this.setData({
                show: false,
                operationId: '',
            });
        },

        onSelect(event) {
            const { operationId } = this.data
            const { action } = event.detail
            action && action(operationId)
        },
    }
})
